<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class PidRule implements Rule
{
    protected $model;

    protected $id;

    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct($model, $id)
    {
        $this->model = $model;
        $this->id = $id;
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        return (($value == 0) || (($value != $this->id) && ($this->model->where('id', $value)->count())));
        if ($value == 0){
            return true;
        }

        if ($value == $this->id){
            return false;
        }

        return !!$this->model->where('id', $value)->count();
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return '所属分类操作有误.';
    }
}
